package com.jdy.haoduoai.adapter;

import android.annotation.SuppressLint;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoai.activity.BaseActivity;
import com.jdy.haoduoai.model.LeSysMessage;
import com.jdy.haoduoai.util.Tools;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

@SuppressLint("InflateParams")
public class MyMessageAdapter extends BaseAdapter {
  private BaseActivity mContext;
  private ArrayList<LeSysMessage> mList;
  private LayoutInflater mInflater;
  private boolean isEditor = false;
  private boolean isSelectAll = false;

  public void setData(ArrayList<LeSysMessage> list) {
    mList = list;
  }

  public MyMessageAdapter(BaseActivity context, ArrayList<LeSysMessage> list) {
    this.mContext = context;
    this.mList = list;
    mInflater = LayoutInflater.from(mContext);
  }

  public void setEditor(boolean iseditor) {
    this.isEditor = iseditor;
  }

  public void setSelectAll(boolean isSelectAll) {
    this.isSelectAll = isSelectAll;
    isEditor = true;
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  public boolean isEditor() {
    return isEditor;
  }

  @Override
  public Object getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final LeSysMessage item = mList.get(position);
    ViewHolder holder;

    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.messageitem, null);
      holder = new ViewHolder();
      holder.title = (TextView) convertView.findViewById(R.id.title);
      holder.newsicon = (ImageView) convertView.findViewById(R.id.newsicon);
      holder.time = (TextView) convertView.findViewById(R.id.time);
      holder.message = (TextView) convertView.findViewById(R.id.message);
      holder.tosimg = (ImageView) convertView.findViewById(R.id.tosimg);
      holder.radiobtn = (CheckBox) convertView.findViewById(R.id.radiobtn);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.title.setText(item.title);

    TextPaint paint = holder.title.getPaint();
    paint.setFakeBoldText(true);

    if (item.read) {
      holder.newsicon.setVisibility(View.GONE);
    } else {
      holder.newsicon.setVisibility(View.VISIBLE);
    }
    // holder.newsicon.setVisibility(View.VISIBLE);

    if (Tools.isNotEmpty(item.updated_at)) {
      String[] dateArr = item.updated_at.split(" ");
      if (dateArr != null) {
        holder.time.setText(dateArr[0]);
      } else {
        holder.time.setText(item.updated_at);
      }
    } else {
      holder.time.setText(item.updated_at);
    }

    // holder.time.setText(item.updated_at);
    holder.message.setText(item.content);

    holder.radiobtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        item.selected = isChecked; // );
      }
    });

    if (isEditor) { // 处于编辑形态
      holder.tosimg.setVisibility(View.GONE);
      holder.radiobtn.setVisibility(View.VISIBLE);
    } else {
      holder.tosimg.setVisibility(View.VISIBLE);
      holder.radiobtn.setVisibility(View.GONE);
    }
    if (isSelectAll) {
      item.selected = true;
      holder.radiobtn.setChecked(true);
    } else {
      holder.radiobtn.setChecked(item.selected);
    }
    return convertView;
  }

  private static class ViewHolder {
    private TextView title;
    private ImageView newsicon;
    private TextView time;
    private TextView message;
    private ImageView tosimg;
    private CheckBox radiobtn;
  }

}
